package com.hu.common.util.comm;

import lombok.extern.slf4j.Slf4j;
import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
import org.apache.commons.lang3.StringUtils;

/**
 * @author 00355760
 * @功能： 姓名转拼音公共方法
 * @版本： V1
 * @版权： 信息技术有限公司（http://www.mychery.com/）
 * @作者： 磐石战队开发组  00355760
 * @日期： 2024/12/3 10:47
 */
@Slf4j
public class NameConvertPinyinUtil {

    public static String getNamePinyin2(String username) {
        HanyuPinyinOutputFormat initializeFormat = new HanyuPinyinOutputFormat();
        initializeFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        initializeFormat.setVCharType(HanyuPinyinVCharType.WITH_V);
        initializeFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        StringBuilder pinyinBuilder = new StringBuilder();
        char[] chars = username.toCharArray();
        for (char everyone : chars) {
            try {
                String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(everyone, initializeFormat);
                if (pinyinArray != null) {
                    pinyinBuilder.append(pinyinArray[0]);
                } else {
                    pinyinBuilder.append(everyone);
                }
            } catch (BadHanyuPinyinOutputFormatCombination e) {
                pinyinBuilder.append(everyone);
            }
        }
        return pinyinBuilder.toString();
    }

    public static String getNamePinyin(String chineseName) {
        // 将中文姓名的第一个字符作为姓，其余字符作为名
        String surname = chineseName.substring(0, 1);
        String givenName = chineseName.substring(1);

        // 将姓和名转换为拼音
        String surnamePinyin = toPinyin(surname, true);
        String givenNamePinyin = toPinyin(givenName, false);
        givenNamePinyin = StringUtils.capitalize(givenNamePinyin);

        // 返回拼音格式的姓名
        return surnamePinyin + " " + givenNamePinyin;
    }

    // 拼音转换核心方法
    private static String toPinyin(String text, boolean isSurname) {
        HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
        format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        format.setVCharType(HanyuPinyinVCharType.WITH_V);
        StringBuilder result = new StringBuilder();
        for (char c : text.toCharArray()) {
            try {
                String[] pinyins = PinyinHelper.toHanyuPinyinStringArray(c, format);
                if (pinyins != null && pinyins.length > 0) {
                    String selected = pinyins[0];
                    if (isSurname) {
                        // 首字母大写
                        selected = StringUtils.capitalize(selected);
                    }
                    result.append(selected);
                }
            } catch (BadHanyuPinyinOutputFormatCombination e) {
                result.append(c);
            }
        }
        return result.toString();
    }
}
